package com.ideal.manage.esms.service.monitor;

import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.common.collect.Tuple;

import java.math.BigDecimal;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class Utils {

    public static double toDouble(Double number) {
        if (number == null) {
            return 0;
        }
        if (number.isNaN()) {
            return 0;
        }
        return number;
    }

    public static Tuple<LocalDateTime, LocalDateTime> calDateRange(String ranges, String startTime, String endTime) {
        LocalDateTime zeroHour = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        LocalDateTime endHour = null;
        // 根据时间范围
        switch (ranges) {
            case "day":
                endHour = zeroHour.plusHours(23).withMinute(59).withSecond(59);
                break;
            case "week":
                endHour = zeroHour;
                zeroHour = zeroHour.minusWeeks(1).withMinute(59).withSecond(59);
                break;
            case "month":
                endHour = zeroHour;
                zeroHour = zeroHour.minusMonths(1).withMinute(59).withSecond(59);
                break;
            case "year":
                endHour = zeroHour;
                zeroHour = zeroHour.minusYears(1).withMinute(59).withSecond(59);
                break;
            case "custom":
                zeroHour = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                endHour = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                break;
            default:
                return null;
        }
        return new Tuple<>(zeroHour, endHour);
    }

    public static JSONObject getFifteenMinutes() {
        JSONObject r = new JSONObject();
        LocalDateTime nextTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        for (int i = 0; i < 97; i++) {
            r.put(nextTime.format(DateTimeFormatter.ofPattern("dd HH:mm")), 0);
            nextTime = nextTime.plusMinutes(15);
        }
        return r;
    }

    public static JSONObject nowHour() {
        JSONObject r = new JSONObject();
        LocalDateTime nextTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(nextTime, now);

        for (int i = 0; i < duration.toHours() + 1; i++) {
            r.put(nextTime.format(DateTimeFormatter.ofPattern("yyy-MM-dd HH")), 0);
            nextTime = nextTime.plusHours(1);
        }
        return r;
    }

    public static JSONObject getHour() {
        return Utils.getHour(0);
    }

    public static JSONObject getHour(Object defaultData) {
        JSONObject r = new JSONObject();
        LocalDateTime nextTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        for (int i = 0; i < 25; i++) {
            r.put(nextTime.format(DateTimeFormatter.ofPattern("yyy-MM-dd HH")), defaultData);
            nextTime = nextTime.plusHours(1);
        }
        return r;

    }

    public static JSONObject getDay(String ranges) {
        JSONObject r = new JSONObject();
        LocalDateTime nextTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        Calendar calendar = Calendar.getInstance();
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (ranges.equals("week")) {
            days = 7;
        }
        for (int i = 0; i < days; i++) {
            r.put(nextTime.format(DateTimeFormatter.ofPattern("yyy-MM-dd")), 0);
            nextTime = nextTime.minusDays(1);
        }
        return r;
    }

    /**
     * 不需要当天
     * @param ranges
     * @return
     */
    public static JSONObject getDayExitToday(String ranges) {
        JSONObject r = new JSONObject();
        LocalDateTime nextTime = LocalDateTime.of(LocalDate.now().minusDays(1), LocalTime.of(0, 0, 0));
        Calendar calendar = Calendar.getInstance();
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (ranges.equals("week")) {
            days = 7;
        }
        for (int i = 0; i < days; i++) {
            r.put(nextTime.format(DateTimeFormatter.ofPattern("yyy-MM-dd")), 0);
            nextTime = nextTime.minusDays(1);
        }
        return r;
    }

    public static JSONObject getMonth() {
        JSONObject r = new JSONObject();
        LocalDateTime nextTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        for (int i = 0; i < 12; i++) {
            r.put(nextTime.format(DateTimeFormatter.ofPattern("yyyy-MM")), 0);
            nextTime = nextTime.minusMonths(1);
        }
        return r;
    }

    public static Tuple<Date, Date> getTodayRange() {
        LocalDateTime zeroHour = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        LocalDateTime endHour = zeroHour.plusHours(23).withMinute(59).withSecond(59);

        Date startDate = Date.from(zeroHour.toInstant(ZoneOffset.of("+8")));
        Date endDate = Date.from(endHour.toInstant(ZoneOffset.of("+8")));
        return new Tuple<>(startDate, endDate);
    }

    public static Double convertShowVolt(Double volt) {
        if (volt == null) {
            return 0.0;
        }
        return Utils.halfUp(volt * 0.001);
    }

    public static double halfUp(Double number) {
        if (number == null) {
            return 0;
        }
        if(number.isInfinite()||number.isNaN()){
            return 0;
        }
        return new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
